//
//  ArticleViewController.m
//  yigou
//
//  Created by SunChao on 17/3/2.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "ArticleViewController.h"
#import "ContentListCell.h"
@interface ArticleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataSource;

@property (assign, nonatomic) NSInteger page;
@property (assign, nonatomic) BOOL pageToEnd;

@end

@implementation ArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageToEnd = NO;
    self.page = 1;
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.tableFooterView = [[UIView alloc]init];
    [self addRefreshHeader:self.tableView];
    [self addLoadMoreFooter:self.tableView];

    [self loadNetworkData];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapTableView)];
//    [self.tableView addGestureRecognizer:tap];
}
//- (void)TapTableView{
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"HiddenCommentBar" object:nil];
//}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [ViewFactory setNavigationBlack:self];
    if ([self.type isEqualToString:@"00"]) {
        self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"我的文章"];
    }else{
        self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"他的文章"];
    }
    
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(goPop)];
    self.navigationItem.leftBarButtonItem = backBtn;
}

//返回
- (void)goPop
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark TableViewDelegate &  DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 440;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ContentListCell *cell = GET_CELL(ContentListCell,@"ContentListCell");
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.row];
    
    [cell contentWithDic:[dic objectForKey:@"content"]];
    
    return cell;
}

#pragma -mark 数据请求

- (void)refresh {
    [super refresh];
    [self.dataSource removeAllObjects];
    [self loadNetworkData];
}

- (void)loadNetworkData{
    self.page = 1;
    [self.dataSource removeAllObjects];
    self.dataSource = [NSMutableArray new];
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = [NSString stringWithFormat:@"%@/hyContent/getContentListByUserIdAndType?contentType=%@&find_user_id=%@&page=%@&limit=%@", APIYIGOU,@"01",self.userId,@"1",@"5"];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        NSDictionary *data = [dict objectForKey:@"data"];
        NSArray *array = [data objectForKey:@"list"];
        if (array.count == 0) {
//             [ViewFactory showSuccess:@"没有数据！"];
            [self showNoData];
            [self.tableView.mj_header endRefreshing];
        }else{
             [self showNoData];
        }
        self.dataSource = [NSMutableArray arrayWithArray:array];
        
        __weak __typeof(self) weakself= self;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [weakself.tableView reloadData];
                [weakself.tableView.header endRefreshing];
                
            });
        
        
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
        [self.tableView.header endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        [self.tableView.header endRefreshing];
        
    }];
}


- (void)loadMore {
    [super loadMore];
    
    DYBaseRequest *request = [DYBaseRequest new];
    NSString *url = nil;
    long limit = 5;
    NSString *type = @"01";
    self.page+=1;
    url = [NSString stringWithFormat:@"%@/hyContent/getContentListByUserIdAndType?contentType=%@&find_user_id=%@&page=%ld&limit=%ld", APIYIGOU,type,self.userId,self.page,limit];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    request.requestUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        NSDictionary *dict = [NSDictionary changeType:request.responseJSONObject];
        NSDictionary *data = [dict objectForKey:@"data"];
        NSArray *addArray = [data objectForKey:@"list"];
        if (addArray.count>0) {
            
            for (NSDictionary *tmpDic in addArray) {
                [self.dataSource addObject:tmpDic];
            }
        
        }else{
            
            [ViewFactory showInfoMsg:@"没有下一页了！"];
            
        }
        
        __weak __typeof(self) weakself= self;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [weakself.tableView reloadData];
            [weakself.tableView.header endRefreshing];
            
        });
    } error:^(YTKBaseRequest *request) {
        NSData *JSONData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
        [ViewFactory showErrorStrMsg:[responseJSON objectForKey:@"message"]];
        [self.tableView.header endRefreshing];
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
        [self.tableView.header endRefreshing];
        
    }];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
     [[NSNotificationCenter defaultCenter]postNotificationName:@"HiddenCommentBar" object:nil];
}
@end
